/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kurvykurvy.ui;

import com.kurvykurvy.image.ImageFrameComponent;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import javax.swing.JComponent;
import javax.swing.event.MouseInputListener;

/**
 *
 * @author HY
 */
public class ImageFrameOverlay
    extends JComponent
    implements MouseInputListener
{
    ImageFrameComponent parent;

    Rectangle[] rotationSpots = new Rectangle[4];
    int rotationHotSpotSize = 10;
    int rotationHotSpotOffset = 10;

    Rectangle[] resizeSpots = new Rectangle[ 8 ];
    int resizeHotSpotSize = 5;
    int resizeHotSpotOffset = 0;


    private ImageFrameOverlay( ImageFrameComponent component )
    {
        if( component == null )
            throw new IllegalArgumentException( "Cannot create ImageFrameOverlay for null component" );
        
        parent = component;
    }

    public static ImageFrameOverlay getImageFrameOverlay( ImageFrameComponent component )
    {
        return new ImageFrameOverlay( component );
    }

    public void paint( Graphics g )
    {
        Graphics2D g2 = (Graphics2D) g;

        AffineTransform originalTransform = g2.getTransform();
        AffineTransform parentTransform = parent.getTransformComponent();

        if( parentTransform != null )
        {
            g2.setTransform( parentTransform );
            createRotationSpots();
            for( int i = 0; i < rotationSpots.length; i++ )
            {
                g2.draw( rotationSpots[ i ] );
            }

            createResizeSpots();
            for( int i = 0; i < resizeSpots.length; i++ )
            {
                g2.draw(  resizeSpots[ i ] );
            }
        }
        
        g2.setTransform( originalTransform );
    }

    private void createRotationSpots()
    {
        Dimension imageSize = parent.getSize();
        rotationSpots[ 0 ] = new Rectangle( rotationHotSpotOffset, rotationHotSpotOffset, rotationHotSpotSize, rotationHotSpotSize );
        rotationSpots[ 1 ] = new Rectangle( imageSize.width - ( rotationHotSpotOffset + rotationHotSpotSize ), rotationHotSpotOffset, rotationHotSpotSize, rotationHotSpotSize );
        rotationSpots[ 2 ] = new Rectangle( rotationHotSpotOffset, imageSize.height - ( rotationHotSpotOffset + rotationHotSpotSize ), rotationHotSpotSize, rotationHotSpotSize );
        rotationSpots[ 3 ] = new Rectangle( imageSize.width - ( rotationHotSpotOffset + rotationHotSpotSize ), imageSize.height - ( rotationHotSpotOffset + rotationHotSpotSize ), rotationHotSpotSize, rotationHotSpotSize );
    }

    private void createResizeSpots()
    {
        Dimension imageSize = parent.getSize();
        resizeSpots[ 0 ] = new Rectangle( resizeHotSpotOffset, resizeHotSpotOffset, resizeHotSpotSize, resizeHotSpotSize );
        resizeSpots[ 1 ] = new Rectangle( ( imageSize.width / 2 ) - resizeHotSpotSize, resizeHotSpotOffset, resizeHotSpotSize, resizeHotSpotSize );
        resizeSpots[ 2 ] = new Rectangle( imageSize.width - resizeHotSpotSize, resizeHotSpotOffset, resizeHotSpotSize, resizeHotSpotSize );
        resizeSpots[ 3 ] = new Rectangle( resizeHotSpotOffset, ( imageSize.height / 2 ) - resizeHotSpotSize, resizeHotSpotSize, resizeHotSpotSize );
        resizeSpots[ 4 ] = new Rectangle( imageSize.width - resizeHotSpotSize, ( imageSize.height / 2 ) - resizeHotSpotSize, resizeHotSpotSize, resizeHotSpotSize );
        resizeSpots[ 5 ] = new Rectangle( resizeHotSpotOffset, imageSize.height - resizeHotSpotSize, resizeHotSpotSize, resizeHotSpotSize );
        resizeSpots[ 6 ] = new Rectangle( ( imageSize.width / 2 ) - resizeHotSpotSize, imageSize.height - resizeHotSpotSize, resizeHotSpotSize, resizeHotSpotSize );
        resizeSpots[ 7 ] = new Rectangle( imageSize.width - resizeHotSpotSize, imageSize.height - resizeHotSpotSize, resizeHotSpotSize, resizeHotSpotSize );

    }

    public void mouseClicked( MouseEvent e )
    {
        parent.mouseClicked( e );
    }

    public void mousePressed( MouseEvent e )
    {
        parent.mousePressed( e );
    }

    public void mouseReleased( MouseEvent e )
    {
        parent.mouseReleased( e );
    }

    public void mouseEntered( MouseEvent e )
    {
        parent.mouseEntered( e );
    }

    public void mouseExited( MouseEvent e )
    {
        parent.mouseExited( e );
    }

    public void mouseDragged( MouseEvent e )
    {
        parent.mouseDragged( e );
    }

    public void mouseMoved( MouseEvent e )
    {
        parent.mouseMoved( e );
    }
}
